package com.example.shopspring.Service;


import com.example.shopspring.Mapper.CartMapper;
import com.example.shopspring.Mapper.ProductMapper;
import com.example.shopspring.Mapper.UserMapper;
import com.example.shopspring.Model.Cart;
import com.example.shopspring.Model.CartProduct;
import com.example.shopspring.Model.Product;
import com.example.shopspring.Model.User;
import com.example.shopspring.util.ErrorResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {

    private final UserMapper userMapper;


    private final ProductMapper productMapper;


    private final CartMapper cartMapper;

    public UserService(UserMapper userMapper, CartMapper cartMapper,ProductMapper productMapper) {
        this.userMapper = userMapper;
        this.cartMapper = cartMapper;
        this.productMapper=productMapper;
    }

    public ErrorResponse login(User user) {
        User ano_user=userMapper.selectByusername(user.getUsername());
        if(ano_user==null){
            return new ErrorResponse("用户名错误");
        }
        if(!user.getPassword().equals(ano_user.getPassword())){
            return new ErrorResponse("密码错误");
        }
        return null;
    }

    public ErrorResponse addCart(Cart cart) {
        Cart user_cart=cartMapper.selectCartByCart(cart.getProductId(),cart.getUsername());
        if(user_cart == null){
            cartMapper.insert(cart);
        }
        else {
            int total_num=cart.getNum()+user_cart.getNum();
            if(total_num>99){
                return new ErrorResponse("在购物车中，该商品只能放置99个");
            }
            user_cart.setNum(total_num);
            cartMapper.updateById(user_cart);
        }
        return null;
    }

    public List<CartProduct> getCartList(String username) {
        List<Cart> cartList = cartMapper.selectListByusername(username);
        List<CartProduct> cartProductList = new ArrayList<>();
        for(Cart cart : cartList){
            Product product=productMapper.selectById(cart.getProductId());
            CartProduct cartProduct=new CartProduct(cart,product);
            cartProductList.add(cartProduct);
        }
        return cartProductList;
    }

    public ErrorResponse deleteCart(int id) {
        int i = cartMapper.deleteById(id);
        if(i ==0){
            return new ErrorResponse("删除失败");
        }
        return null;
    }

//    public ErrorResponse updateCart(int id, int num) {
//        Cart cart=cartMapper.selectById(id);
//        if(cart==null){
//            return new ErrorResponse("该商品不在您的购物车中");
//        }
//        if(num<0){
//            return new ErrorResponse("数量不可以为负");
//        }
//        if(num>99){
//            return new ErrorResponse("在购物车中，该商品只能放置99个");
//        }
//        cart.setNum(num);
//        cartMapper.updateById(cart);
//        return null;
//    }

//    public ErrorResponse buyProducts(List<CartProduct> cartProducts) {
//        for(CartProduct cartProduct : cartProducts){
//            cartMapper.deleteById(cartProduct.getId());
//        }
//        return null;
//    }


    public List<CartProduct> getCartProducts(Integer[] ids) {
        List<CartProduct> cartProductList = new ArrayList<>();
        for(Integer id : ids){
            Cart cart=cartMapper.selectById(id);
            Product product=productMapper.selectById(cart.getProductId());
            CartProduct cartProduct=new CartProduct(cart,product);
            cartProductList.add(cartProduct);
        }
        return cartProductList;
    }

    public ErrorResponse register(User user) {
        User ano_user=userMapper.selectByusername(user.getUsername());
        if(ano_user!=null){
            return new ErrorResponse("用户名已存在");
        }
        userMapper.insert(user);
        return null;
    }

    public ErrorResponse updateCart(int id, int num) {
        Cart cart=cartMapper.selectById(id);
        if(cart==null){
            return new ErrorResponse("该商品不在您的购物车中");
        }
        if(num<0){
            return new ErrorResponse("数量不可以为负");
        }
        if(num>99){
            return new ErrorResponse("在购物车中，该商品只能放置99个");
        }
        cart.setNum(num);
        cartMapper.updateById(cart);
        System.out.println(111);
        return null;
    }
}
